﻿using System;
using System.Collections.Generic;
using Bricks;
using Bricks.Injecting;
using Microsoft.Practices.ServiceLocation;

namespace Motar.Desktop.Injecting
{

    /// <summary>
    /// 
    /// </summary>
    public class ServiceLocatorFacade : FacadeBase<IContainerFacade>, IServiceLocator
    {

        /// <summary>
        /// 
        /// </summary>
        /// <param name="containerFacade"></param>
        public ServiceLocatorFacade(IContainerFacade containerFacade) :
            base(() => containerFacade)
        {
            if (containerFacade == null)
            {
                throw new ArgumentNullException("containerFacade");
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public IEnumerable<TService> GetAllInstances<TService>()
        {
            return Execute<IEnumerable<TService>>(containerFacade => containerFacade.ResolveAll<TService>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public IEnumerable<object> GetAllInstances(Type serviceType)
        {
            return Execute<IEnumerable<object>>(containerFacade => containerFacade.ResolveAll(serviceType));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <param name="key"></param>
        /// <returns></returns>
        public TService GetInstance<TService>(string key)
        {
            return Execute<TService>(containerFacade => containerFacade.Resolve<TService>(key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <typeparam name="TService"></typeparam>
        /// <returns></returns>
        public TService GetInstance<TService>()
        {
            return Execute<TService>(containerFacade => containerFacade.Resolve<TService>());
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public object GetInstance(Type serviceType, string key)
        {
            return Execute<object>(containerFacade => containerFacade.Resolve(serviceType, key));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public object GetInstance(Type serviceType)
        {
            return Execute<object>(containerFacade => containerFacade.Resolve(serviceType));
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="serviceType"></param>
        /// <returns></returns>
        public object GetService(Type serviceType)
        {
            return Execute<object>(containerFacade => containerFacade.Resolve(serviceType));
        }
    }
}
